package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.MaterialOrderMapper;
import com.hd.pzhwlxt.pojo.MaterialOrder;
import com.hd.pzhwlxt.pojo.Materialprice;
import com.hd.pzhwlxt.service.MaterialOrderService;
import com.hd.pzhwlxt.service.MaterialpriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @create: 2022-09-26 9:02
 * @author: 怀念天空
 * @Description:
 */

@Service
public class MaterialOrderServiceImpl extends ServiceImpl<MaterialOrderMapper, MaterialOrder> implements MaterialOrderService {

    @Autowired
    private MaterialOrderMapper materialOrderMapper;

    @Autowired
    private MaterialpriceService materialpriceService;

    @Override
    public int insertSelective(MaterialOrder record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(MaterialOrder record) {
        return baseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<MaterialOrder> getAllOrder() {
        return materialOrderMapper.getAllOrder();
    }

    @Override
    public Page<MaterialOrder> getAllOrderPage(Integer pageNum, Integer pageSize) {
        return materialOrderMapper.getAllOrderPage(new Page<>(pageNum, pageSize));
    }

    @Override
    public boolean addMaterialOrder(MaterialOrder materialOrder) {
        Integer materialId = materialOrder.getMaterialId();
        QueryWrapper<Materialprice> wrapper = new QueryWrapper<>();
        wrapper.eq("material_id", materialId);
        Materialprice materialprice = materialpriceService.getOne(wrapper);
        Double outPrice = materialprice.getOutPrice();
        materialOrder.setMoney(BigDecimal.valueOf(outPrice * materialOrder.getNum()));
        return materialOrderMapper.insert(materialOrder) > 0;
    }
}
